cmake_minimum_required(VERSION 3.24)

enable_language(C ASM)

set(SystemDefines
    USE_STDPERIPH_DRIVER
    STM32F10X_MD
    $<$<CONFIG:Debug>:DEBUG>
)

set(CMSIS_IncludeDir
    ${CMAKE_CURRENT_SOURCE_DIR}/CMSIS/Core/Include
    ${CMAKE_CURRENT_SOURCE_DIR}/CMSIS/Core/Include/a-profile
    ${CMAKE_CURRENT_SOURCE_DIR}/CMSIS/Core/Include/m-profile
    ${CMAKE_CURRENT_SOURCE_DIR}/CMSIS/Core/Include/r-profile
)
set(StdPeriphLib_IncludeDir ${CMAKE_CURRENT_SOURCE_DIR}/inc)
set(StdPeriphLib_SourceDir ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(StdPeriphLib_SourceFiles
    ${StdPeriphLib_SourceDir}/misc.c
    ${StdPeriphLib_SourceDir}/stm32f10x_adc.c
    ${StdPeriphLib_SourceDir}/stm32f10x_bkp.c
    ${StdPeriphLib_SourceDir}/stm32f10x_can.c
    ${StdPeriphLib_SourceDir}/stm32f10x_cec.c
    ${StdPeriphLib_SourceDir}/stm32f10x_crc.c
    ${StdPeriphLib_SourceDir}/stm32f10x_dac.c
    ${StdPeriphLib_SourceDir}/stm32f10x_dbgmcu.c
    ${StdPeriphLib_SourceDir}/stm32f10x_dma.c
    ${StdPeriphLib_SourceDir}/stm32f10x_exti.c
    ${StdPeriphLib_SourceDir}/stm32f10x_flash.c
    ${StdPeriphLib_SourceDir}/stm32f10x_fsmc.c
    ${StdPeriphLib_SourceDir}/stm32f10x_gpio.c
    ${StdPeriphLib_SourceDir}/stm32f10x_i2c.c
    ${StdPeriphLib_SourceDir}/stm32f10x_iwdg.c
    ${StdPeriphLib_SourceDir}/stm32f10x_pwr.c
    ${StdPeriphLib_SourceDir}/stm32f10x_rcc.c
    ${StdPeriphLib_SourceDir}/stm32f10x_rtc.c
    ${StdPeriphLib_SourceDir}/stm32f10x_sdio.c
    ${StdPeriphLib_SourceDir}/stm32f10x_spi.c
    ${StdPeriphLib_SourceDir}/stm32f10x_tim.c
    ${StdPeriphLib_SourceDir}/stm32f10x_usart.c
    ${StdPeriphLib_SourceDir}/stm32f10x_wwdg.c
    ${CMAKE_CURRENT_SOURCE_DIR}/startup_stm32f10x_md.s
)

# Library interface includes and definitions
add_library(stm32 INTERFACE)
target_include_directories(stm32 INTERFACE ${StdPeriphLib_IncludeDir} ${CMSIS_IncludeDir})
target_compile_definitions(stm32 INTERFACE ${SystemDefines})

# STM32Drivers static library
add_library(STM32Drivers STATIC)
target_sources(STM32Drivers PRIVATE ${StdPeriphLib_SourceFiles})
target_link_libraries(STM32Drivers PUBLIC stm32)
set_target_properties(STM32Drivers PROPERTIES
    C_STANDARD 11
    C_STANDARD_REQUIRED ON
    C_EXTENSIONS OFF
)

# Add the map file to the list of files to be removed with 'clean' target
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES ADDITIONAL_CLEAN_FILES ${CMAKE_PROJECT_NAME}.map)
